package ym.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ym.mapper.RepairReportMapper;
import ym.pojo.RepairReport;
import ym.service.RepairReportService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class RepairReportServiceImpl implements RepairReportService {
    @Autowired
    private RepairReportMapper repairReportMapper;

    @Override
    public void add(RepairReport repairReport) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String timeNow = sdf.format(new Date());
        repairReport.setRepairReportSubmitTime(timeNow);
        repairReportMapper.insert(repairReport);
    }

    @Override
    public void update(RepairReport repairReport) {
        repairReportMapper.updateById(repairReport);
    }

    @Override
    public void delete(Integer id) {
        repairReportMapper.deleteById(id);
    }

    @Override
    public RepairReport findById(Integer id) {
        return repairReportMapper.selectById(id);
    }

    @Override
    public List<RepairReport> findAll() {
        return repairReportMapper.selectList(null);
    }

    @Override
    public Page<RepairReport> search(String search, int page, int size) {
        QueryWrapper<RepairReport> queryWrapper = new QueryWrapper<>();
        if (search != null) {
            //queryWrapper.like("", search);
        }
        return repairReportMapper.selectPage(new Page<>(page, size), queryWrapper);
    }

    @Override
    public Page<RepairReport> searchByUserId(Integer userId, String search, int page, int size) {
        QueryWrapper<RepairReport> queryWrapper = new QueryWrapper<>();
        if (search != null) {
            queryWrapper.eq("studentId", userId).like("repairReportContent",search);
        } else {
            queryWrapper.eq("studentId", userId);
        }
        return repairReportMapper.selectPage(new Page<>(page, size), queryWrapper);
    }

    @Override
    public Page<RepairReport> searchByMaintainerId(Integer maintainerId, String search, int page, int size) {
        QueryWrapper<RepairReport> queryWrapper = new QueryWrapper<>();
        if (search != null) {
            queryWrapper.eq("maintainerId", maintainerId).like("repairReportContent",search);
        } else {
            queryWrapper.eq("maintainerId", maintainerId);
        }
        return repairReportMapper.selectPage(new Page<>(page, size), queryWrapper);
    }


}
